<?php

/**
 * Controller_event
 */

class Controller_event extends MyApp {

	public function __construct()
	{
		parent::__construct();
        $this->uid = $this->user['user_id'];

	}

	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}

	/** 备忘录列表 **/
	public function actionList()
	{
        $data = $this->db->fetchAll("SELECT * FROM `tbl_event`  where user_id = {$this->uid} ");

        foreach ($data as $key => $value){
            $data[$key]['start'] = date('Y-m-d H:i', $value['starttime']);
            $data[$key]['end'] = date('Y-m-d H:i', $value['endtime']);

        }
        //var_dump($data);
		$this->tpl->display("event/m.event.list.tpl.php");
	}

	/** 添加备忘录 **/
	public function actionAdd()
	{
		$id      = (int)$this->get->id;
		$date    = $this->get->date;
		$enddate = $this->get->end;

		if($date == $enddate) $enddate = '';

		if(empty($enddate)){
			$display = 'style="display:none"';
			$enddate = $date;
			$chk = '';
		}else{
			$display = 'style=""';
			$chk = 'checked';
		}

		$enddate = empty($_GET['end']) ? $date:$_GET['end'];

		$this->tpl->assign("display", $display);
		$this->tpl->assign("chk", $chk);
		$this->tpl->assign("date", $date);
		$this->tpl->assign("enddate", $enddate);
		$this->tpl->assign("id", $id);
		$this->tpl->display("event/m.event.add.tpl.php");
	}

	/** 修改备忘录 **/
	public function actionUpdate()
	{

		$id      = (int)$this->get->id;
		$date    = $this->get->date;
		$enddate = $this->get->end;

		$row = $this->db->fetchRow("SELECT * FROM `tbl_event` WHERE id='{$id}' AND user_id ={$this->uid}");

		if(!empty($row)){
			$id        = $row['id'];
			$title     = $row['title'];
			$starttime = $row['starttime'];
			$start_d   = date("Y-m-d",$starttime);
			$start_h   = date("H", $starttime);
			$start_m   = date("i", $starttime);

			$endtime = $row['endtime'];
			if($endtime == 0){
				$end_d       = $startdate;
				$end_chk     = '';
				$end_display = "style='display:none'";
			}else{
				$end_d   = date("Y-m-d",$endtime);
				$end_h   = date("H",$endtime);
				$end_m   = date("i",$endtime);
				$end_chk = "checked";
				$end_display = "style=''";
			}

			$allday = $row['allday'];

			if($allday == 1){
				$display    = "style='display:none'";
				$allday_chk = "checked";
			}else{
				$display    = "style=''";
				$allday_chk = '';
			}
		}

		$this->tpl->assign("allday_chk", $allday_chk);
		$this->tpl->assign("allday", $allday);
		$this->tpl->assign("end_chk", $end_chk);
		$this->tpl->assign("end_display", $end_display);
		$this->tpl->assign("start_d", $start_d);
		$this->tpl->assign("start_h", $start_h);
		$this->tpl->assign("start_m", $start_m);
		$this->tpl->assign("end_d", $end_d);
		$this->tpl->assign("end_h", $end_h);
		$this->tpl->assign("end_m", $end_m);
		$this->tpl->assign("starttime", $starttime);
		$this->tpl->assign("title", $title);
		$this->tpl->assign("display", $display);
		$this->tpl->assign("chk", $chk);
		$this->tpl->assign("date", $date);
		$this->tpl->assign("enddate", $enddate);
		$this->tpl->assign("id", $id);
		$this->tpl->display("event/m.event.update.tpl.php");
	}

	/**  **/
	public function actionGetJson()
	{
		$data = $this->db->fetchAll("SELECT * FROM `tbl_event`  where user_id = {$this->uid} ");

		foreach ($data as $key => $value){
			$data[$key]['start'] = date('Y-m-d H:i', $value['starttime']);
			$data[$key]['end'] = date('Y-m-d H:i', $value['endtime']);

		}

		echo json_encode($data);
	}


	public function actionDoAdd()
	{
		$events = stripslashes(trim($_POST['event']));//事件内容
		$events = strip_tags($events); //过滤HTML标签，并转义特殊字符

		$isallday = $_POST['isallday'];//是否是全天事件
		$isend    = $_POST['isend'];//是否有结束时间

		$startdate = trim($_POST['startdate']);//开始日期
		$enddate   = trim($_POST['enddate']);//结束日期

		$s_time = $_POST['s_hour'].':'.$_POST['s_minute'].':00';//开始时间
		$e_time = $_POST['e_hour'].':'.$_POST['e_minute'].':00';//结束时间

//		if($isallday==1 && $isend==1){
//			$starttime = strtotime($startdate);
//			$endtime   = strtotime($enddate);
//		}elseif($isallday==1 && $isend==""){
//			$starttime = strtotime($startdate);
//		}elseif($isallday=="" && $isend==1){
//			$starttime = strtotime($startdate.' '.$s_time);
//			$endtime   = strtotime($enddate.' '.$e_time);
//		}else{
//			$starttime = strtotime($startdate.' '.$s_time);
//		}
        $starttime =$startdate;
		$colors = array("#360","#f30","#06c");
		$key    = array_rand($colors);
		$color  = $colors[$key];

		$isallday = $isallday?1:0;
		$query = $this->db->query("insert into `tbl_event` (`title`,`starttime`,`endtime`,`allday`,`color`,`user_id`) values ('$events','$starttime','$endtime','$isallday','$color', '{$this->uid}' )");
		if($this->db->insert_id() > 0){
			echo '1';
		}else{
			echo '写入失败！';
		}
	}

	public function actionDoUpdate()
	{
		$id = intval($_POST['id']);
		if($id==0){
			echo '事件不存在！';
			exit;
		}
		$events = stripslashes(trim($_POST['event']));//事件内容
		$events = strip_tags($events); //过滤HTML标签，并转义特殊字符

		$isallday = $_POST['isallday'];//是否是全天事件
		$isend    = $_POST['isend'];//是否有结束时间

		$startdate = trim($_POST['startdate']);//开始日期
		$enddate   = trim($_POST['enddate']);//结束日期

		$s_time = $_POST['s_hour'].':'.$_POST['s_minute'].':00';//开始时间
		$e_time = $_POST['e_hour'].':'.$_POST['e_minute'].':00';//结束时间

		if($isallday==1 && $isend==1){
			$starttime = strtotime($startdate);
			$endtime = strtotime($enddate);
		}elseif($isallday==1 && $isend==""){
			$starttime = strtotime($startdate);
			$endtime = 0;
		}elseif($isallday=="" && $isend==1){
			$starttime = strtotime($startdate.' '.$s_time);
			$endtime = strtotime($enddate.' '.$e_time);
		}else{
			$starttime = strtotime($startdate.' '.$s_time);
			$endtime = 0;
		}

		$isallday = $isallday?1:0;
		$this->db->query("update `tbl_event` set `title`='$events' where `id`='$id' AND  user_id = {$this->uid} ");//,`starttime`='$starttime',`endtime`='$endtime',`allday`='$isallday'
		if($this->db->affected_rows()==1){
			echo '1';
		}else{
			echo '出错了！';
		}
        Helper_Client::location("修改成功","index.php?a=event.list");

        //$this->tpl->display("event/m.event.list.tpl.php");
	}

	public function actionDoDrag()
	{
		$id       = $_POST['id'];
		$daydiff  = (int)$_POST['daydiff']*24*60*60;
		$minudiff = (int)$_POST['minudiff']*60;
		$allday   = $_POST['allday'];
		$row      = $this->db->fetchRow("select * from `tbl_event` where id='$id' AND  user_id = {$this->uid} ");
		//echo $allday;exit;
		if($allday == "true"){
			if($row['endtime']==0){
				$sql = "update `tbl_event` set starttime=starttime+'$daydiff' where id='$id' AND  user_id = {$this->uid} ";
			}else{
				$sql = "update `tbl_event` set starttime=starttime+'$daydiff',endtime=endtime+'$daydiff' where id='$id' AND  user_id = {$this->uid} ";
			}

		}else{
			$difftime = $daydiff + $minudiff;
			if($row['endtime'] == 0){
				$sql = "update `tbl_event` set starttime=starttime+'$difftime' where id='$id' AND  user_id = {$this->uid} ";
			}else{
				$sql = "update `tbl_event` set starttime=starttime+'$difftime',endtime=endtime+'$difftime' where id='$id' AND  user_id = {$this->uid} ";
			}
		}
		$result = $this->db->query($sql);
		if($this->db->affected_rows()==1){
			echo '1';
		}else{
			echo '出错了！';
		}
	}

	public function actionDoDel()
	{
		$id = intval($_POST['id']);
		if($id > 0){
			$this->db->query("delete from `tbl_event` where `id`='$id' AND  user_id = {$this->uid} ");
			if($this->db->affected_rows() == 1){
				echo '1';
			}else{
				echo '出错了！';
			}
		}else{
			echo '事件不存在！';
		}
	}

	public function actionDoResize()
	{
		$id       = $this->post->id;
		$daydiff  = (int)$_POST['daydiff'] * 24 * 60 * 60;
		$minudiff = (int)$_POST['minudiff'] * 60;

		$row = $this->db->fetchRow("select * from `tbl_event` where id='$id' AND  user_id = {$this->uid} ");
		//echo $allday;exit;
		$difftime = $daydiff + $minudiff;
		if($row['endtime']==0){
			$sql = "update `tbl_event` set endtime=starttime+'$difftime' where id='$id' and  user_id = {$this->uid} ";
		}else{
			$sql = "update `tbl_event` set endtime=endtime+'$difftime' where id='$id' AND  user_id = {$this->uid} ";
		}

		$result = $this->db->query($sql);
		if($this->db->affected_rows() == 1){
			echo '1';
		}else{
			echo '出错了！';
		}
	}

}

?>